package com.zx.idc.backend.gui.tlog;


import com.baomidou.mybatisplus.plugins.Page;
import com.zx.idc.backend.gui.common.BasicBaseApi;
import com.zx.idc.backend.gui.common.Result;
import com.zx.idc.backend.gui.shiro.TokenManagerUtil;
import com.zx.idc.ds.tlog.entity.TLogActive;
import com.zx.idc.ds.tlog.service.ITLogActiveService;
import com.zx.idc.ds.tlog.vo.TLogActiveVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 活动日志 前端控制器
 * </p>
 *
 * @author 陈冬
 * @since 2019-04-09
 */
@RestController
@RequestMapping("/api/tlogActive")
public class TlogActiveController extends BasicBaseApi<ITLogActiveService, TLogActive> {

    @Autowired
    private TokenManagerUtil tokenManagerUtil;

    @Autowired
    private SessionDAO sessionDAO;

    @GetMapping("/query-page")
    @RequiresPermissions("transaction-log:t-log-active")
    public Result<Page<TLogActiveVo>> page(Page<TLogActiveVo> page, TLogActiveVo tlogActiveVo) {
        return success(service.pageByParam(page, tlogActiveVo));
    }

    /**
     * 终止此该用户登录会话
     *
     * @param id 记录的id
     */
    @GetMapping("/logout-session/{id}")
    @RequiresPermissions("transaction-log:t-log-active")
    public Result<Boolean> logoutSession(@PathVariable("id") String id) {

        Session session = null;
        try {
            session = sessionDAO.readSession(id);
            sessionDAO.delete(session);
        } catch (UnknownSessionException e) {
            log.error("当前用户会话找不到,不进行session删除操作", e.getMessage());
        }

        try {
            return success(service.deleteById(id));
        } catch (Exception e) {
            log.error("终止此该用户登录会话操作： 数据库删除异常");
        }
        return error("终止此该用户登录会话失败");

    }
}

